<?php
include_once('common/header.php');
if($AppUI->id > 0) {
	Vfriend_Function::redirect(HOST_VFRIEND);	
}
$intType 	= $objRequest->getParam('ptypelogin', 0);
if($intType == 0) {
	$strEmail 			= $objRequest->getParam('pemail_header', '');
	$strPassword 		= $objRequest->getParam('ppassword_header', '');
	$intRemember 		= $objRequest->getParam('prememberme_header', 0);
	$intPasswordLength 	= $objRequest->getParam('ppassword_hlength', 0);
}
else {
	$strEmail 			= $objRequest->getParam('pemail', '');
	$strPassword 		= $objRequest->getParam('ppassword', '');
	$intRemember 		= $objRequest->getParam('prememberme', 0);
	$intPasswordLength 	= $objRequest->getParam('ppassword_length', 0);
}
if($strEmail && $strPassword) {
	$objVfriendUser 	= new Vfriend_User();
	$strDbPassword		= $objVfriendUser->getPassword($strEmail);
	$strChapPassword	= md5(md5(session_id()) . $strDbPassword);	
	if($strChapPassword == $strPassword) {
		session_regenerate_id();
		$objAuth = new Vfriend_Auth();
		if($AppUI = $objAuth->authVfriend($strEmail)) {
			$objVfriendUser->updateLastlogin($AppUI->id);			
			if($intRemember) {
				$strCookies = $strEmail . '::' . md5($strDbPassword) . '::' . $intPasswordLength . '::1';
				$strCookies = Vfriend_Function::base64E($strCookies);
				Vfriend_Function::setCookie(COOKIE_VFRIEND_AUTH, $strCookies);								
			}
			Vfriend_Function::redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_VFRIEND);
			exit();
		}
	}
	unset($_COOKIE[COOKIE_VFRIEND_AUTH]);
	unset($_COOKIE['LOGIN_PASS']);
	$strBaseEmail = Vfriend_Function::base64E($strEmail);
	Vfriend_Function::redirect(LINK_INDEX_LOGIN_ERROR . '?code=' . $strBaseEmail);
} 
exit();
?>